<?php

$dbhost = "mysql.groovemechanic.net";
$dbuser = "oproto_user";
$dbpass = "oproto12345";
$dbname = "groovemechanic_oproto";

$username = trim($_POST['username']);

$con = mysql_connect ($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db ($dbname) or die (mysql_error());

if ($username == "") {
	$query = "SELECT * FROM documents WHERE 1 ORDER BY username, name";
} else {
	$query = "SELECT * FROM documents WHERE username='$username' ORDER BY username, name";
}

$result = mysql_query($query) or die(mysql_error());

$curUsername = "";
while ($row = mysql_fetch_array($result)) {
	
	if ($row[username] != $curUsername) {
		if ($curUsername != "") {
			$result_str .= "</optgroup>\n";
		}
		$result_str .= "<optgroup label='$row[username]'>\n";
		$curUsername = $row[username];
	}
	
	
	$result_str .= "<option value='" . trim($row[name]) . "&" . trim($row[username]) . "'>" . 
		trim($row[name]) . "</option>\n";
}
$result_str .= "</optgroup>\n";

echo $result_str;

mysql_close($con);

?>
